package com.qiantu.phone.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.LLPwdUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;

public class SetPwdActivity extends AppActivity implements TextWatcher{

    private com.qiantu.phone.widget.EditTextLayout mEditText1;
    private com.qiantu.phone.widget.EditTextLayout mEditText2;
    private android.widget.TextView complete;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_pwd;
    }

    @Override
    protected void initView() {
        mEditText1 = findViewById(R.id.edit_text1);
        mEditText2 = findViewById(R.id.edit_text2);
        complete = findViewById(R.id.complete);
        setOnClickListener(complete);
        mEditText1.getEditText().addTextChangedListener(this);
        mEditText2.getEditText().addTextChangedListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == complete){
            if (!mEditText1.getText().toString().equals(mEditText2.getText().toString())){
                toast(R.string.two_msg_not_equal);
                return;
            }
            if (!LLPwdUtil.checkPwd(mEditText1.getText().toString())){
                toast(R.string.pwd_error_hint);
                return;
            }
            LLHttpManager.setPwd(this, mEditText1.getText().toString(), mEditText2.getText().toString(), new HttpCallback<HttpData<Void>>(getAppActivity()){
                @Override
                public void onSucceed(HttpData<Void> result) {
                    super.onSucceed(result);
                    LoginActivity.start(getAppActivity(), AppApplication.getInstance().getUserPhone(), "");
//                    AppApplication.getInstance().getLoginInfo().getUserInfo().setHasPassword(true);
//                    AppApplication.getInstance().updateLoginInfo();
//                    setResult(RESULT_OK);
//                    finish();
                }
            });
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(mEditText1.getText()) || TextUtils.isEmpty(mEditText2.getText())){
            complete.setEnabled(false);
        }else {
            complete.setEnabled(true);
        }
    }
}